// 登录与注册处理

import { getUserInfoAPI } from "@/api";

const state = {
  token: "",
  userInfo: {}, //用户信息对象
};
const mutations = {
  //跟新token的 mutation 函数
  UPDATETOKEN(state, newToken) {
    state.token = newToken;
  },
  // 获取用户信息
  UPDATEUSERINFO(state, info) {
    state.userInfo = info;
  },
  //退出登录清除用户信息
  CLEAR(state) {
    state.token = "";
    state.userInfo = {};
  },
};
const actions = {
  // 初始化用户信息
  async initUserInfo({ commit }) {
    const res = await getUserInfoAPI();
    if(res.status == 0){
      commit("UPDATEUSERINFO", res.data);
    }

  },
  // 退出登录
  async userLogout({ commit }) {
    commit("CLEAR");
  },
};

const getters = {};
export default {
  state,
  mutations,
  actions,
  getters,
};
